package silo.dungeonbuddy;

import java.awt.Color;
import java.awt.Graphics2D;

public class ScrollBar
{
	private int x;
	private int y;
	private int width;
	private int length;
	private boolean vertical;
	private int position;
	private IntValue value;
	private int maxValue;
	
	public ScrollBar(int eX, int eY, int eWidth, int eLength, boolean eVertical, IntValue eValue, int eMaxValue)
	{
		x=eX;
		y=eY;
		width=eWidth;
		length=eLength;
		vertical=eVertical;
		value=eValue;
		maxValue=eMaxValue;
		position=0;
	}//constructor
	
	public void draw(Graphics2D g)
	{
		if(vertical)
		{
			g.setColor(Color.lightGray);
			g.fillRect(x,y,width,length);
			g.setColor(Color.darkGray);
			g.drawLine(x+1, y+1, x+1, y+length-1);
			g.drawLine(x+1, y+1, x+width-1, y+1);
			g.setColor(Color.gray);
			g.drawLine(x+2, y+1, x+2, y+length-1);
			g.drawLine(x+2, y+2, x+width-1, y+2);
			g.setColor(Color.black);
			g.drawRect(x,y,width,length);
			g.setColor(Color.gray);
			g.fillOval(x+1, y+1+position, width-1, width-1);
			g.setColor(Color.black);
			g.drawOval(x+1, y+1+position, width-2, width-2);
		}else
		{
			g.setColor(Color.lightGray);
			g.fillRect(x,y,length,width);
			g.setColor(Color.darkGray);
			g.drawLine(x+1, y+1, x+1, y+width-1);
			g.drawLine(x+1, y+1, x+length-1, y+1);
			g.setColor(Color.gray);
			g.drawLine(x+2, y+1, x+2, y+width-1);
			g.drawLine(x+2, y+2, x+length-1, y+2);
			g.setColor(Color.black);
			g.drawRect(x,y,length,width);
			g.setColor(Color.gray);
			g.fillOval(x+1+position, y+1, width-1, width-1);
			g.setColor(Color.black);
			g.drawOval(x+1+position, y+1, width-2, width-2);
		}
	}//draw
	
	public void function(Start sender)
	{
		if(vertical)
		{
			position=(int)Math.abs((float)length * ((float)value.getValue()/(float)maxValue));
			if((sender.getMouseX()>x)&&(sender.getMouseX()<(x+width))&&(sender.getMouseY()>(y+position))&&(sender.getMouseY()<(y+position+width))&&(!sender.getScrolling())&&(sender.getMouseLeft()))
			{
				sender.setScrolling(true);
				sender.setSelectedScrollBar(this);
			}else if((sender.getScrolling())&&(!sender.getMouseLeft()))
			{
				sender.setScrolling(false);
				sender.setSelectedScrollBar(null);
			}else if((sender.getScrolling())&&(sender.getSelectedScrollBar()==this))
			{
				if(((sender.getMouseY()-y)>0)&&((sender.getMouseY()-y)<(length-1-width)))
				{
					position=sender.getMouseY()-y-(width/2);
				}else if(((sender.getMouseY()-y)<=0)&&(Math.abs(sender.getMouseX()-(x+(width/2)))<30))
				{
					position=0;
				}
				value.setValue(0-(int)(maxValue*(float)((float)position/(float)length)));
			}//if(sender.getScrolling())
		}else//if(vertical)
		{
			position=(int)Math.abs((float)length * ((float)value.getValue()/(float)maxValue));
			if((sender.getMouseY()>y)&&(sender.getMouseY()<(y+width))&&(sender.getMouseX()>(x+position))&&(sender.getMouseX()<(x+position+width))&&(!sender.getScrolling())&&(sender.getMouseLeft()))
			{
				sender.setScrolling(true);
				sender.setSelectedScrollBar(this);
			}else if((sender.getScrolling())&&(!sender.getMouseLeft()))
			{
				sender.setScrolling(false);
				sender.setSelectedScrollBar(null);
			}else if((sender.getScrolling())&&(sender.getSelectedScrollBar()==this))
			{
				if(((sender.getMouseX()-x)>0)&&((sender.getMouseX()-x)<(length-1-width)))
				{
					position=sender.getMouseX()-x-(width/2);
				}else if(((sender.getMouseX()-x)<=0)&&(Math.abs(sender.getMouseY()-(y+(width/2)))<30))
				{
					position=0;
				}
				value.setValue(0-(int)(maxValue*(float)((float)position/(float)length)));
			}//if(sender.getScrolling())
		}//else -- if(vertical)
	}//function(Start sender)
	
	public void function(Form sender)
	{
		if(vertical)
		{
			position=(int)Math.abs((float)length * ((float)value.getValue()/(float)maxValue));
			if((sender.getMouseX()>x)&&(sender.getMouseX()<(x+width))&&(sender.getMouseY()>(y+position))&&(sender.getMouseY()<(y+position+width))&&(!sender.getScrolling())&&(sender.getMouseLeft()))
			{
				sender.setScrolling(true);
				sender.setSelectedScrollBar(this);
			}else if((sender.getScrolling())&&(!sender.getMouseLeft()))
			{
				sender.setScrolling(false);
				sender.setSelectedScrollBar(null);
			}else if((sender.getScrolling())&&(sender.getSelectedScrollBar()==this))
			{
				if(((sender.getMouseY()-y)>0)&&((sender.getMouseY()-y)<(length-1-width)))
				{
					position=sender.getMouseY()-y-(width/2);
				}else if(((sender.getMouseY()-y)<=0)&&(Math.abs(sender.getMouseX()-(x+(width/2)))<30))
				{
					position=0;
				}
				value.setValue(0-(int)(maxValue*(float)((float)position/(float)length)));
			}//if(sender.getScrolling())
		}else//if(vertical)
		{
			position=(int)Math.abs((float)length * ((float)value.getValue()/(float)maxValue));
			if((sender.getMouseY()>y)&&(sender.getMouseY()<(y+width))&&(sender.getMouseX()>(x+position))&&(sender.getMouseX()<(x+position+width))&&(!sender.getScrolling())&&(sender.getMouseLeft()))
			{
				sender.setScrolling(true);
				sender.setSelectedScrollBar(this);
			}else if((sender.getScrolling())&&(!sender.getMouseLeft()))
			{
				sender.setScrolling(false);
				sender.setSelectedScrollBar(null);
			}else if((sender.getScrolling())&&(sender.getSelectedScrollBar()==this))
			{
				if(((sender.getMouseX()-x)>0)&&((sender.getMouseX()-x)<(length-1-width)))
				{
					position=sender.getMouseX()-x-(width/2);
				}else if(((sender.getMouseX()-x)<=0)&&(Math.abs(sender.getMouseY()-(y+(width/2)))<30))
				{
					position=0;
				}
				value.setValue(0-(int)(maxValue*(float)((float)position/(float)length)));
			}//if(sender.getScrolling())
		}//else -- if(vertical)
	}//function(Form sender)
	
	//SETTERS
	public void setMaxValue(int eVal)
	{
		maxValue=eVal;
	}
}//class ScrollBar